//
//  CollectionViewController.m
//  ToYourHeartNews
//
//  Created by XJing on 16/1/29.
//  Copyright © 2016年 www.lanou3g.com 蓝鸥. All rights reserved.
//

#import "CollectionViewController.h"
#import "DateBaseManager.h"
#import "VideoCollectionCell.h"
#import "AllDetailModel.h"
#import <UIImageView+WebCache.h>
#import "NewsDataBaseManager.h"
#import "Videomodel.h"
#import "NewsCollectionCell.h"
@interface CollectionViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)UITableView *newsTableView;
@property (nonatomic, strong)NSMutableArray *dataArr;//视频数组
@property (nonatomic, strong)NSMutableArray *newsArr;//新闻数组
@property (nonatomic, strong)UIScrollView *scroll;//大的scroll
@property (nonatomic, strong)UIView *blank;//按钮栏
@end

@implementation CollectionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"收藏";
    //设置编辑按钮
    UIButton *edit = [UIButton buttonWithType:UIButtonTypeSystem];
    [edit setTitle:@"编辑" forState:UIControlStateNormal];
    [edit setTitle:@"完成" forState:UIControlStateSelected];
     edit.frame = CGRectMake(0, 0, 50, 50);
    [edit addTarget:self action:@selector(editAction:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:edit];
    //上面的按钮栏
    self.blank = [[UIView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, 30)];
    _blank.backgroundColor = [UIColor grayColor];
   
    //创建两个button
    //视频按钮
    UIButton *videoBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    videoBtn.frame = CGRectMake(self.view.frame.size.width / 4, 5, self.view.frame.size.width / 8, 20);
    [videoBtn setTitle:@"视频" forState:UIControlStateNormal];
    videoBtn.backgroundColor = [UIColor redColor];
    [videoBtn addTarget:self action:@selector(videoAction) forControlEvents:UIControlEventTouchUpInside];
    [_blank addSubview:videoBtn];
    //新闻按钮
    UIButton *newsBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    CGRect f = videoBtn.frame;
    newsBtn.backgroundColor = [UIColor redColor];
    [newsBtn setTitle:@"新闻" forState:UIControlStateNormal];
    f.origin.x = self.view.frame.size.width / 4 * 3;
    newsBtn.frame = f;
    [newsBtn addTarget:self action:@selector(newsAction) forControlEvents:UIControlEventTouchUpInside];
    [_blank addSubview:newsBtn];
    [self.view addSubview:_blank];
    
    //scrollView各收藏分类
    self.scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, _blank.frame.origin.y + _blank.frame.size.height, self.view.frame.size.width, self.view.frame.size.height)];
    _scroll.backgroundColor = [UIColor yellowColor];
    _scroll.contentSize = CGSizeMake(2 * self.view.frame.size.width, self.view.frame.size.height);
    _scroll.bounces = NO;
    _scroll.pagingEnabled = YES;
    _scroll.delegate = self;
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self.view addSubview:_scroll];
    //视频页
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0 , self.view.frame.size.width, _scroll.frame.size.height) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource= self;
//    _tableView.backgroundColor = [UIColor yellowColor];
    [_tableView registerClass:[VideoCollectionCell class] forCellReuseIdentifier:@"collectonCell"];
    [_scroll addSubview:_tableView];
    //新闻页
    self.newsTableView = [[UITableView alloc]initWithFrame:CGRectMake(self.view.frame.size.width, 0, self.view.frame.size.width, _scroll.frame.size.height)];
    _newsTableView.delegate = self;
    _newsTableView.dataSource= self;
    [_newsTableView registerClass:[NewsCollectionCell class] forCellReuseIdentifier:@"newsCell"];
    [_scroll addSubview:_newsTableView];
    //从数据库中获取数据
    [[DateBaseManager shareInstance]openDB];
    self.dataArr = [[DateBaseManager shareInstance]select];
    //从新闻数据库中获取数据
    [[NewsDataBaseManager shareInstance]openDB];
    self.newsArr = [[NewsDataBaseManager shareInstance]select];
    
    
}
-(void)videoAction
{
    //把按钮栏射程属性 设置y
    _scroll.contentOffset = CGPointMake(0,0);
}
-(void)newsAction
{
    _scroll.contentOffset = CGPointMake(self.view.frame.size.width,0);
}
//tableView是否允许编辑
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
//编辑按钮触发的方法
-(void)editAction:(UIButton *)btn
{
    btn.selected = !btn.selected;
    [_tableView setEditing:btn.selected animated:YES];
}
//根据按钮的状态 设置tableView是否处于编辑状态
-(void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    [_tableView setEditing:editing animated:animated];
}
//通过tableView的编辑状态 判断执行什么操作
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        //执行数据库删除
        Videomodel *m = _dataArr[indexPath.row];
        [[DateBaseManager shareInstance]deleteVideoWithId:m.videoId];
        //从当前的数据数组中移除
        [_dataArr removeObjectAtIndex:indexPath.row];
        //视图同步
        [_tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if ([tableView isEqual:_tableView]) {
        return _dataArr.count;
    }
    if ([tableView isEqual:_newsTableView]) {
        return _newsArr.count;
    }else{
        return 0;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return self.view.frame.size.height / 5;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([tableView isEqual:_tableView]) {
        VideoCollectionCell *cell = [tableView dequeueReusableCellWithIdentifier:@"collectonCell" forIndexPath:indexPath];
        if (_dataArr.count != 0) {
            Videomodel *m = _dataArr[indexPath.row];
            cell.titleLable.text = m.title;
            
            //将时间转换成00:00形式
            NSNumber *n = m.length;
            if (n.integerValue < 60) {
                cell.timeLable.text = [NSString stringWithFormat:@"00: %ld", n.integerValue];
            }else{
                NSInteger fen = n.integerValue / 60;
                NSInteger miao = n.integerValue - 60 * fen;
                NSString *time = [NSString stringWithFormat:@"%ld分%ld秒", fen, miao];
                cell.timeLable.text = time;
            }
            cell.replyLable.text = [m.replyCount.stringValue stringByAppendingString:@"评论"];
            [cell.picture sd_setImageWithURL:[NSURL URLWithString:m.cover]];
        }
        return cell;

    }
    if ([tableView isEqual:_newsTableView]) {
        NewsCollectionCell *cell = [tableView dequeueReusableCellWithIdentifier:@"newsCell"];
        AllDetailModel *m = _newsArr[indexPath.row];
        cell.contentLable.text = m.title;
        cell.writerLable.text = m.auther_name;
        cell.timeLable.text = m.date;
        return cell;
    }else{
        return 0;
    }
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
